
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/user_prop_model/list_by_propId_count_model.dart';
import 'package:yysk_app/model/user_prop_model/list_by_propId_model.dart';
import 'package:yysk_app/net/api/api_url.dart';
import 'package:yysk_app/net/http.dart';

class PropListController extends GetxController {
    final scrollController = ScrollController();
      int pageNum = 1;
  int pageSize = 10;
  var count = 0.obs;//数量
   bool showBtm = false; //是否开启转增
  final list = RxList<ListByPropIdItem>([]);
  late String propId;
  late String propName;

    @override
  void onInit() {
    super.onInit();
    final params = Get.parameters ?? {};
    final args = Get.arguments ?? {};
    propId = params['propId'] ?? args['propId'];
    propName = params['propName'] ?? args['propName'];
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        getPropList(loadMore: true);
        getCount();
      }
    });
    getPropList();
    getCount();
 
  }
  @override
  void onClose(){
        super.onClose();
        scrollController.dispose();
  }
  getCount() {
    http.netFetch<ListByPropIdCountModel>(
      ApiUrl.countByPropId,
      params: {'propId': propId},
      success: (result) {
        count.value = result.data!.count ?? 0;
          showBtm = result.data!.supplyTransferable!;
      },
    );
  }

  getPropList({loadMore = false}) {
    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      http.netFetch<ListByPropIdModel>(
        ApiUrl.listByPropId,
        params: {'pageNum': pageNum, 'pageSize': 20, 'propId': propId},
        success: (result) {
          List<ListByPropIdItem> items = result.data?.list ?? [];
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            list.clear();
          }
            list.addAll(items);
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNum--;
      }
    }
  }

}